<template>
  <div class="certificate">
    <el-upload
      class="avatar-uploader"
      :drag="drag"
      name="file"
      :show-file-list="showFileList"
      :data="{
        authorization: uploadData.auth,
        policy: uploadData.policy
      }"
      :before-upload="beforeUpload"
      :action="uploadData.host ? uploadData.host : ''"
      :on-success="upLoadSuccess"
      :on-error="onError"
    >
      <el-image v-if="url" class="avatar" :src="url" fit="contain"></el-image>
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
  </div>
</template>
<script>
export default {
  data() {
    return {
      imageUrl:'',
      uploadData: {}
    };
  },
  props: {
    // 是否支持拖拽
    drag: {
      type: Boolean,
      default: true
    },
    // 上传图片类型
    floder: {
      type: Number,
      default: 0
    },
    // 是否显示已上传文件列表
    showFileList: {
      type: Boolean,
      default: true
    },
    // 图片地址
    url: {
      type: String,
      default: ""
    }
  },
  methods: {
    async beforeUpload(file) {
      let data = { floder: this.floder };
      const res = await this.$api.common.UploadAuthImgUpyunConfig(data);
      if (res) {
        this.uploadData = res.data;
      }
    },
    upLoadSuccess(response, file, fileList) {
      this.$emit("update:url", this.uploadData.domain + response.url);
      this.imageUrl=''
      this.$message.success("上传成功");
    },
    onError(err) {
      this.$message.error("上传失败，请重试");
    }
  }
};
</script>
<style scoped lang="scss">
.width180 {
  width: 180px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
